#ifndef DOCUMENTWIDGET_H
#define DOCUMENTWIDGET_H
//***************************************************************************************************************
// Filename: documentwidget.h
// head file for documentwidget
// this class is a qt widget
// which can load a pdf file
// and display it by use QListWidget
// date March 2012
//***************************************************************************************************************
#include <QLabel>
#include <QRectF>
#include <poppler-qt4.h>
#include <QListWidget>
#include <QVector>
#include <QKeyEvent>
#include <QLineF>
#include <QPointF>
#include <QMouseEvent>
#include <QScrollBar>


class DocumentWidget : public QListWidget
{
    Q_OBJECT

public:
    DocumentWidget(QWidget *parent = 0);
    ~DocumentWidget();

    int numPages();
    Poppler::Document* getDoc();

    //func that returns a vector if QImage of pages
    QVector<QImage> getPages();
    // run mouse move distance
    double getDistance();
    // get mouse rotaton degrees
    int getDegree();
    // get mouse efftice degree 
    int getEDegree();
    // get key press times
    int getUpKey();
    int getDownKey();
    int getPageUpKey();
    int getPageDownKey();
    
    public slots:
    // slot to setDocument
    // take file path
    // and return if load file or not
    bool setDocument(const QString &filePath);
    // slot to set page
    // if page != -1 , set current page to page -1 
    void setPage(int page = -1);
    // set scale
    // if scale is -1 , use fit window mode
    void setScale(qreal scale);

    // slot to beign and stop test 
    void beginTest();
    void abortTest();


  protected:
    //Function click event
    // a function to override QListWigdet's key press event 
    void keyPressEvent (QKeyEvent *);
    
    //Function handle mouse move event
    // if mouse moved track distance
    void mouseMoveEvent (QMouseEvent *);
    // event filter to filt mouse move event on child widget
    bool eventFilter(QObject *obj, QEvent *event);
   
    //a function to prevent QListWigdet's key press event
    void mousePressEvent (QMouseEvent * );
    
    //Function handle double click event
    // if a double click , and test start , check if clicked on 
    void mouseDoubleClickEvent(QMouseEvent * );

    // function handle wheel event
    // this function will traking all the wheel rotation
    void wheelEvent(QWheelEvent *event);

    //Function handle resize event
    //if widget is resized recompute the gridand call update()
    void resizeEvent ( QResizeEvent *);
    

private slots:
   //priavte slot that handle scrollbar change
   //if value changes, check where is current page
   void scrollBarChangeHandler(int value);
  signals:
   //page changes signal
   void pageChanged(int currentPage);
   //page click signal
   void pageClicked(int currentPage);
    
private:
    void showPage(int page = -1);
    Poppler::Document *doc;
    int currentPage;
    qreal scaleFactor;

    QLabel *current;
    QVector<QImage> pages;
    QVector<int> positions;

    // following varibales are used for get testing data
    double travelDistance;
    bool startTest;
    QPointF last;
    QScrollBar *scrollBar;
    int numDegrees;
    int eNumDegrees;
    int upkey;
    int downkey;
    int pageupkey;
    int pagedownkey;
    
};

#endif
